import 'dart:convert';

import 'package:dk_flutter_base/dk_ui.dart';

///缓存操作类型
enum CacheOptionType {
  put, //0,put, //单个存储（key-value）
  asyncPut, //1,asyncPut, //异步单存储
  multiPut, //2,multiPut, //多存储
  asyncMultiPut, //3,asyncMultiPut, //异步多存储
  get, //4,get, //单个获取
  multiGet, //5,multiGet, //多获取 value类型单一
  //multiGetWithMultiType, //6,multiGetWithMultiType, //多获取 每个value类型不一定一致
  remove, //6,remove, //单清除
  asyncRemove, //7,asyncRemove, //异步单清除
  multiRemove, //8,multiRemove, //多清除
  asyncMultiRemove, //9,asyncMultiRemove, //异步多清除
  clear, //10,clear, //清除所有
  asyncClear, //11,asyncClear, //异步清除所有
}

/*///缓存值的类型
enum CacheValueType {
  file,
}*/

class CacheOption {
  ///处理缓存类型
  CacheOptionType type;

  ///查询或者删除的key集合
  List<String> keyList;

  ///增加或者修改的key-value
  Map<String, dynamic> map;

  ///是否异步处理
  bool handleAsync;

  CacheOption.fromJson(Map<String, dynamic> json) {
    int jsonType = json['type'] as int;
    if (null != jsonType &&
        jsonType >= 0 &&
        jsonType < CacheOptionType.values.length) {
      this.type = CacheOptionType.values[jsonType];
    }
    this.keyList = (json['keyList'] as List) != null
        ? (json['videoList'] as List).map((i) => i.toString()).toList()
        : null;
    this.map = (json['map'] as Map) != null
        ? (json['map'] as Map).map((dynamic key, dynamic value) {
            return MapEntry<String, dynamic>(key?.toString(), value);
          })
        : null;
    this.handleAsync = json['handleAsync'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['type'] = this.type?.index;
    data['keyList'] = this.keyList;
    data['map'] = map;
    data['handleAsync'] = this.handleAsync;
    return data;
  }

  /*CacheValueType valueType;
  Map<String, CacheValueType> keyTypeMap;*/

  CacheOption.put({@required String key, @required dynamic value}) {
    map ??= {key: value is String ? value : json.encode(value)};
    type = CacheOptionType.put;
    handleAsync = false;
  }

  CacheOption.asyncPut({@required String key, @required dynamic value}) {
    map ??= {key: value is String ? value : json.encode(value)};
    type = CacheOptionType.asyncPut;
    handleAsync = true;
  }

  CacheOption.multiPut({@required this.map}) {
    type = CacheOptionType.multiPut;
    handleAsync = false;
  }

  CacheOption.asyncMultiPut({@required this.map}) {
    type = CacheOptionType.asyncMultiPut;
    handleAsync = true;
  }

  CacheOption.get({@required String key}) {
    keyList ??= [key];
    type = CacheOptionType.get;
  }

  CacheOption.multiGet({@required this.keyList}) {
    type = CacheOptionType.multiGet;
  }

  /*CacheOption.multiGetWithMultiType(
      {@required this.keyTypeMap, this.valueType}) {
    type = CacheOptionType.multiGetWithMultiType;
  }*/

  CacheOption.remove({@required String key}) {
    keyList ??= [key];
    type = CacheOptionType.remove;
    handleAsync = false;
  }

  CacheOption.asyncRemove({@required String key}) {
    keyList ??= [key];
    type = CacheOptionType.asyncRemove;
    handleAsync = true;
  }

  CacheOption.multiRemove({@required this.keyList}) {
    type = CacheOptionType.multiRemove;
    handleAsync = false;
  }

  CacheOption.asyncMultiRemove({@required this.keyList}) {
    type = CacheOptionType.asyncMultiRemove;
    handleAsync = true;
  }

  CacheOption.clear() {
    type = CacheOptionType.clear;
    handleAsync = false;
  }

  CacheOption.asyncClear() {
    type = CacheOptionType.asyncClear;
    handleAsync = true;
  }
}

class HandleCacheResultBean {
  bool putResult;
  String getResult;
  Map<String, String> getMultiResult;
  bool removeResult;
  bool clearResult;

  HandleCacheResultBean.fromJson(Map<String, dynamic> json) {
    this.putResult = json["putResult"];
    this.getResult = json["getResult"];
    this.getMultiResult = (json['getMultiResult'] as Map) != null
        ? (json['getMultiResult'] as Map).map((dynamic key, dynamic value) {
            return MapEntry<String, String>(key?.toString(), value?.toString());
          })
        : null;
    this.removeResult = json["removeResult"];
    this.clearResult = json["clearResult"];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['putResult'] = this.putResult;
    data['getResult'] = this.getResult;
    data['getMultiResult'] = this.getMultiResult;
    data['removeResult'] = this.removeResult;
    data['clearResult'] = this.clearResult;
    return data;
  }
}
